package com.bw.cashier.page

import android.graphics.Bitmap
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.cashier.R
import com.bw.cashier.databinding.ActivityCodeChargeBinding
import com.bw.common.utils.DialogUtils
import com.bw.common.view.BaseMvvmActivity
import com.google.zxing.BarcodeFormat
import com.google.zxing.MultiFormatWriter
import com.google.zxing.common.BitMatrix
import com.journeyapps.barcodescanner.BarcodeEncoder
import dagger.hilt.android.AndroidEntryPoint

/**
 * 二维码收费页面
 */
@Route(path = RouterPath.PATH_CODE_CHARGE_PATH)
@AndroidEntryPoint
class CodeChargeActivity : BaseMvvmActivity<ActivityCodeChargeBinding>() {
    @JvmField
    @Autowired
    var id =""
    @JvmField
    @Autowired
    var name = ""
    @JvmField
    @Autowired
    var price = ""
    @JvmField
    @Autowired
    var address = ""
    override fun getLayoutId(): Int = R.layout.activity_code_charge

    override fun initViews() {
        ARouter.getInstance().inject(this)
        super.initViews()
        //返回
        mDataBinding.back.setOnClickListener { finish() }
        mDataBinding.tvHouseInfo.setText(address)
        mDataBinding.tvOwner.setText("户主：${name}")
        mDataBinding.tvChargeAmount.setText("￥${price}")

        //点击收费
        mDataBinding.btnForward.setOnClickListener {
            DialogUtils.showLoading(this , "账单生成中...")
            ToastUtils.showLong("转发成功！！！")
            SPUtils.getInstance().put("sid",id)
            ARouter.getInstance().build(RouterPath.PATH_CHARGE_DETAIL_PATH)
                .navigation()
        }
        mDataBinding.ivChargeCode.setImageBitmap(generateQRCode("支付成功"))
    }



    //二维码
    private fun generateQRCode(content: String): Bitmap? {
        val bitMatrix: BitMatrix = MultiFormatWriter().encode(
            content, BarcodeFormat.QR_CODE, 200, 200)
        val barcodeEncoder = BarcodeEncoder().createBitmap(bitMatrix)


        return barcodeEncoder
    }

    //销毁
    override fun onDestroy() {
        super.onDestroy()
        DialogUtils.dismissLoading()
    }

}